package org.wsdltools.profiles.az


import org.wsdltools.common.model.MEPEnum;
import org.wsdltools.common.model.ServiceInteraction;
import org.wsdltools.common.model.Operation;

class SchemaGenerator {
	public String generateResponderSchema(ServiceInteraction si) {
		use (ServiceInteractionWsdlDSL, OperationWsdlDSL) {
			
			StringWriter xsd = new StringWriter()
			def xmlBuilder = new groovy.xml.MarkupBuilder(xsd)
			
			// Create map of namespace alias declarations
			def rootElementAttributes = [
					'xmlns:xs': 'http://www.w3.org/2001/XMLSchema',
					'xmlns:tns': si.responderServiceSchemaTargetNamespace,
					'xmlns:d': si.coreSchemaNamespace,
					targetNamespace: si.responderServiceSchemaTargetNamespace,
					elementFormDefault: 'qualified',
					attributeFormDefault: 'unqualified',
					version: si.version]
			
			
			xmlBuilder.'xs:schema'(rootElementAttributes) {			
				// Import core schema			
				'xs:import' (schemaLocation : "../${si.coreSchemaDirName}/${si.coreSchemaName}", namespace : si.coreSchemaNamespace)
				
				'xs:element' (name: si.name, type: "tns:${si.name}Type")
				if (si.responder.mep == MEPEnum.InOut) {
					'xs:element' (name: "${si.name}Response", type: "tns:${si.name}ResponseType")
				}
				
				'xs:complexType'(name: "${si.name}Type") {
					'xs:sequence' {
						'xs:element' (name: 'SomeElement', type: 'd:SomeElementType')
						'xs:any' (namespace: '##other', processContents: 'lax', minOccurs: '0', maxOccurs: 'unbounded')
					}
				}
				
				if (si.responder.mep == MEPEnum.InOut) {
					'xs:complexType'(name: "${si.name}ResponseType") {
						'xs:sequence' {
							'xs:element' (name: 'ResultCode', type: 'tns:ResultCodeEnum')
							'xs:element' (name: 'comment', type: 'xs:string', minOccurs: '0')
							'xs:any' (namespace: '##other', processContents: 'lax', minOccurs: '0', maxOccurs: 'unbounded')
						}
					}
					
					'xs:simpleType'(name: "ResultCodeEnum") {
						'xs:restriction' (base: 'xs:string') {
							'xs:enumeration' (value: 'OK')
							'xs:enumeration' (value: 'ERROR')
							'xs:enumeration' (value: 'INFO')
						}
					}
				}
			}
			"${GeneratorUtil.xmlPrologue}${GeneratorUtil.licenseDeclaration}${xsd}"
		}
	}
	
	public String generateInitiatorSchema(ServiceInteraction si) {
		use (ServiceInteractionWsdlDSL, OperationWsdlDSL) {
			
			StringWriter xsd = new StringWriter()
			def xmlBuilder = new groovy.xml.MarkupBuilder(xsd)
			
			// Create map of namespace alias declarations
			def rootElementAttributes = [
					'xmlns:xs': 'http://www.w3.org/2001/XMLSchema',
					'xmlns:tns': si.initiatorServiceSchemaTargetNamespace,
					'xmlns:core': si.coreSchemaNamespace,
					targetNamespace: si.initiatorServiceSchemaTargetNamespace,
					elementFormDefault: 'qualified',
					attributeFormDefault: 'unqualified',
					version: si.version]
			
			
			xmlBuilder.'xs:schema'(rootElementAttributes) {			
				// Import core schema			
				'xs:import' (schemaLocation : "../${si.coreSchemaDirName}/${si.coreSchemaName}", namespace : si.coreSchemaNamespace)
				
				'xs:element' (name: "Process${si.name}Response", type: "tns:Process${si.name}ResponseType")
				
				'xs:complexType'(name: "Process${si.name}ResponseType") {
					'xs:sequence' {
						'xs:element' (name: 'SomeElement', type: 'core:SomeElementType')
						'xs:any' (namespace: '##other', processContents: 'lax', minOccurs: '0', maxOccurs: 'unbounded')
					}
				}
			}
			"${GeneratorUtil.xmlPrologue}${GeneratorUtil.licenseDeclaration}${xsd}"
		}
	}	
	
	public String generateCoreSchema(ServiceInteraction si) {
use (ServiceInteractionWsdlDSL, OperationWsdlDSL) {
			
			StringWriter xsd = new StringWriter()
			def xmlBuilder = new groovy.xml.MarkupBuilder(xsd)
			
			// Create map of namespace alias declarations
			def rootElementAttributes = [
					'xmlns:xs': 'http://www.w3.org/2001/XMLSchema',
					'xmlns:tns': si.coreSchemaNamespace,
					targetNamespace: si.coreSchemaNamespace,
					elementFormDefault: 'qualified',
					attributeFormDefault: 'unqualified',
					version: si.version]
			
			
			xmlBuilder.'xs:schema'(rootElementAttributes) {			
				
				'xs:complexType'(name: "SomeElementType") {
					'xs:sequence' {
						'xs:element' (name: 'SomeElement', type: 'xs:string')
						'xs:any' (namespace: '##other', processContents: 'lax', minOccurs: '0', maxOccurs: 'unbounded')
					}
				}
			}
			"${GeneratorUtil.xmlPrologue}${GeneratorUtil.licenseDeclaration}${xsd}"
		}		
	}
}
